package homebuh.view
{
	import homebuh.model.ApplicationProxy;
	
	import mx.core.Container;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class ApplicationMediator extends Mediator implements IMediator
	{
		public static const NAME:String = 'ApplicationMediator';

		private var appProxy: ApplicationProxy;
		
		public function ApplicationMediator(viewComponent: App) 
		{
			super(NAME, viewComponent);
			
			appProxy = facade.retrieveProxy(ApplicationProxy.NAME) as ApplicationProxy;
			appProxy.viewState = ApplicationProxy.LOGIN_STATE;
			
			facade.registerMediator(new LoginPanelMediator(app.login));
			facade.registerMediator(new AccountsMediator(app.accounts));
		}
		
		override public function listNotificationInterests():Array 
		{
			
			return [ 	
						ApplicationProxy.VIEW_STATE_CHANGED
				   ];
		}

		override public function handleNotification(note: INotification):void
		{
			switch (note.getName()) 
			{
				case ApplicationProxy.VIEW_STATE_CHANGED:
					changeViewState();
					break;
			}
		}	

		private function changeViewState():void
		{
			var child: Container = app.login;
			
			switch (appProxy.viewState) 
			{
				case ApplicationProxy.LOGIN_ERROR_STATE:
			   		app.faultEffect.end();
			   		app.faultEffect.play();	   		
					break;
				
				case ApplicationProxy.LOGGED_IN_STATE:
					child = app.accounts;
					break;
			}			
			app.mainView.selectedChild = child;
		}
		
		protected function get app(): App
		{
			return viewComponent as App;
		}
	}
}